package com.ttc.mBackupK16.BackupContacts;

import java.util.*;

import com.ttc.mBackupK16.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;


public class CustomListAdapter extends BaseAdapter {

	ArrayList<Contact> listContacts;
	int resource;
	Context context;
	public int iNumItemChecked = 0;
	TextView txtContactsNumberChecked;
	public boolean isListFile = false;
	
	public CustomListAdapter(Context context, int textViewResourceId,
			ArrayList<Contact> objects, TextView textViewNum) {


		this.context = context;
		resource = textViewResourceId;
		listContacts = objects;
		txtContactsNumberChecked = textViewNum;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = new CustomRowContact(context);
		}

		final Contact item = listContacts.get(position);

		if (convertView != null) {
			TextView contactNameTextView = ((CustomRowContact) convertView).getDisplayName();
			TextView contactPhoneTextView = ((CustomRowContact) convertView).getPhoneNumber();
			ImageView imgPhoto = ((CustomRowContact) convertView).getImagePhoto();
			
			CheckBox checkbox = ((CustomRowContact) convertView).getChBContactRow();
			checkbox.setChecked(item.isChecked());
			/*checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    item.setChecked(isChecked);
                }                
            });*/
			checkbox.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox temp = (CheckBox)v;
					item.setChecked(temp.isChecked());
                    if(temp.isChecked()){
                    	iNumItemChecked++;
                    }else{
                    	iNumItemChecked--;
                    }
                	txtContactsNumberChecked.setText(iNumItemChecked + "");
				}
			});
			contactNameTextView.setText(item.getStructuredName().getDisplayName());
			if(!item.getPhone().isEmpty()){
				contactPhoneTextView.setText(item.getPhone().get(0).getNumber());
			}else{
				contactPhoneTextView.setText("no phone number");
			}
			
			if(isListFile){
				imgPhoto.setImageResource(R.drawable.ic_json);
			}else{
				Bitmap bm = item.getPhoto();
				if(bm != null){
					imgPhoto.setImageBitmap(bm);
				}else{
					imgPhoto.setImageResource(R.drawable.ic_no_photo);
				}
			}

		}
		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listContacts.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listContacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
